package com.android.project.framework.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DbAdapter {
	
	@SuppressWarnings("unused")
    private static final String LOG_TAG = DbAdapter.class.getSimpleName();
     
    private Context context;
    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;
     

//  Database fields
    private static final String DATABASE_TABLE  = "association";
     
    public static final String KEY_ID   = "_id";
    public static final String KEY_WORD1  = "word1";
    public static final String KEY_WORD2 = "word2";
    public static final String KEY_PESO  = "peso";
   
    public DbAdapter(Context context) {
        this.context = context;
}

	public DbAdapter open() throws SQLException {
	         
	        dbHelper = new DatabaseHelper(context);
	        database = dbHelper.getWritableDatabase();
	        return this;
	}
	
	public void close() {
	         
	        dbHelper.close();
	         
	}
	
	 private ContentValues createContentValues(String word1, String word2, String peso ) {
           
           ContentValues values = new ContentValues();
           values.put( KEY_WORD1, word1 );
           values.put( KEY_WORD2, word2 );
           values.put( KEY_PESO, peso );
           return values;
	 }
	 
	 public long createAssoc(String word1, String word2, String peso ) {
		
         
         ContentValues initialValues = createContentValues(word1, word2, peso);
         return database.insertOrThrow(DATABASE_TABLE, null, initialValues);
	 }
	 
	 public boolean updateAssoc( long ID, String word1, String word2, String peso ) {
         
         ContentValues updateValues = createContentValues( word1, word2, peso);
         return database.update(DATABASE_TABLE, updateValues, KEY_ID + "=" + ID, null) > 0;
          
	 }
	 
	//delete an association      
	 public boolean deleteAssoc(long ID) {
	                  
	     return database.delete(DATABASE_TABLE, KEY_ID + "=" + ID, null) > 0;
	     
	                  
     }
	 
	 public void deleteAll()
	 {
	    
	     database.delete(DATABASE_TABLE, null, null);

	 }
  
         //fetch all association
     public Cursor fetchAllAssoc() {
              
             return database.query(DATABASE_TABLE, new String[] { KEY_ID, KEY_WORD1, KEY_WORD2, KEY_PESO}, null, null, null, null, null);
     }
	          
     
   //fetch association filter by a string
     public Cursor fetchAssocByFilter(String filter) {
                      
    	if(filter.contains("'")){
    		filter.replace("'", "''");
    	}
                     Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                    		 KEY_ID, KEY_WORD1, KEY_WORD2, KEY_PESO },
                    		 	KEY_WORD1 + " like '%"+ filter + "%'", null, null, null, null, null);
              
                     return mCursor;
                      
       }
}
